package org.rsbot.script.methods;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.rsbot.script.wrappers.GEItemInfo;

public class GrandExchange extends MethodProvider
{
  private static final String HOST = "http://services.runescape.com";
  private static final String GET = "/m=itemdb_rs/viewitem.ws?obj=";
  private static final Pattern PATTERN = Pattern.compile("(?i)<td><img src=\".+obj_sprite\\.gif\\?id=(\\d+)\" alt=\"(.+)\"");

  GrandExchange()
  {
    super(null);
  }

  public String getItemName(int paramInt)
  {
    GEItem localGEItem = lookup(paramInt);
    if (localGEItem != null) {
      return localGEItem.getName();
    }
    return "";
  }

  public int getItemID(String paramString)
  {
    GEItem localGEItem = lookup(paramString);
    if (localGEItem != null) {
      return localGEItem.getID();
    }
    return -1;
  }

  public GEItem lookup(int paramInt)
  {
    try
    {
      URL localURL = new URL("http://services.runescape.com/m=itemdb_rs/viewitem.ws?obj=" + paramInt);
      BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(localURL.openStream()));

      int i = 0;
      int j = 0;
      double[] arrayOfDouble = new double[4];
      String str2 = ""; String str3 = "";
      String str1;
      while ((str1 = localBufferedReader.readLine()) != null) {
        if ((str1.contains("<div class=\"brown_box main_ge_page")) && (i == 0))
        {
          if (!str1.contains("vertically_spaced")) {
            return null;
          }
          i = 1;
          localBufferedReader.readLine();
          localBufferedReader.readLine();
          str2 = localBufferedReader.readLine(); continue;
        }if (str1.contains("<img id=\"item_image\" src=\"")) {
          str3 = localBufferedReader.readLine(); continue;
        }if (str1.matches("(?i).+ (price|days):</b> .+")) {
          arrayOfDouble[j] = parse(str1);
          j++; continue;
        }if (str1.matches("<div id=\"legend\">"))
          break;
      }
      return new GEItem(str2, str3, paramInt, arrayOfDouble);
    } catch (IOException localIOException) {
    }
    return null;
  }

  public GEItem lookup(String paramString)
  {
    try
    {
      URL localURL = new URL("http://services.runescape.com/m=itemdb_rs/results.ws?query=" + paramString + "&price=all&members=");

      BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(localURL.openStream()));
      String str;
      while ((str = localBufferedReader.readLine()) != null) {
        if (str.contains("<div id=\"search_results_text\">")) {
          str = localBufferedReader.readLine();
          if (str.contains("Your search for"))
            return null;
        }
        if (str.startsWith("<td><img src=")) {
          Matcher localMatcher = PATTERN.matcher(str);
          if ((localMatcher.find()) && 
            (localMatcher.group(2).contains(paramString)))
            return lookup(Integer.parseInt(localMatcher.group(1)));
        }
      }
    }
    catch (IOException localIOException)
    {
    }
    return null;
  }

  private double parse(String paramString) {
    if ((paramString != null) && (!paramString.isEmpty())) {
      paramString = stripFormatting(paramString);
      paramString = paramString.substring(paramString.indexOf(':') + 2, paramString.length());
      paramString = paramString.replace(",", "");
      if (!paramString.endsWith("%")) {
        if ((!paramString.endsWith("k")) && (!paramString.endsWith("m"))) {
          return Double.parseDouble(paramString);
        }
        return Double.parseDouble(paramString.substring(0, paramString.length() - 1)) * (paramString.endsWith("m") ? 1000000 : 1000);
      }

      int i = paramString.startsWith("+") ? 1 : -1;
      paramString = paramString.substring(1);
      return Double.parseDouble(paramString.substring(0, paramString.length() - 1)) * i;
    }
    return -1.0D;
  }

  private String stripFormatting(String paramString) {
    if ((paramString != null) && (!paramString.isEmpty()))
      return paramString.replaceAll("(^[^<]+>|<[^>]+>|<[^>]+$)", "");
    return "";
  }

  @Deprecated
  public GEItemInfo loadItemInfo(int paramInt)
  {
    return null;
  }

  public static class GEItem
  {
    private final String name;
    private final String examine;
    private final int id;
    private final int guidePrice;
    private final double change30;
    private final double change90;
    private final double change180;

    GEItem(String paramString1, String paramString2, int paramInt, double[] paramArrayOfDouble)
    {
      this.name = paramString1;
      this.examine = paramString2;
      this.id = paramInt;
      this.guidePrice = (int)paramArrayOfDouble[0];
      this.change30 = paramArrayOfDouble[1];
      this.change90 = paramArrayOfDouble[2];
      this.change180 = paramArrayOfDouble[3];
    }

    public double getChange30Days()
    {
      return this.change30;
    }

    public double getChange90Days()
    {
      return this.change90;
    }

    public double getChange180Days()
    {
      return this.change180;
    }

    public int getID()
    {
      return this.id;
    }

    public int getGuidePrice()
    {
      return this.guidePrice;
    }

    public String getName()
    {
      return this.name;
    }

    public String getDescription()
    {
      return this.examine;
    }
    @Deprecated
    public int getMarketPrice() {
      return this.guidePrice;
    }
    @Deprecated
    public int getMaxPrice() {
      return this.guidePrice;
    }
    @Deprecated
    public int getMinPrice() {
      return this.guidePrice;
    }
  }
}